<?

include_once("cmsConfig.php");
include_once("pxaccess.inc");

// function for creating the inital transaction, and running the request
function createTransaction($txnData, $db){
	//create the object in the database
	
	$tableWriter = new CmsTableWriter(CMS_FORMAT,$db);	

	
	$http_host   = getenv("HTTP_HOST");
	$request_uri = getenv("REQUEST_URI");
	$server_url  = "http://$http_host";
	$script_url  = "$server_url/$request_uri";	
	  
	$MerchantReference = $tableWriter->db->getId('contenttablesequence');
	$tableWriter->begin();
	$tableWriter->addField($tableWriter->primaryKey, $MerchantReference);
	$TxnType = "Purchase";
	$tableWriter->addField("title",$TxnType);
	$tableWriter->addField("formid",7);
	$tableWriter->addField("documenttypecode","docPx");
	$tableWriter->addField("shortdescription","PROCESSING");
	$tableWriter->addField("value", $txnData['AmountInput']);
	$tableWriter->addField("url",$txnData['TxnData1']);
	$tableWriter->addField("author", $txnData['TxnData2']);
	$tableWriter->addField("address",$txnData['TxnData3']);
	$tableWriter->addField("email",$txnData['EmailAddress']);
  
	if ( $tableWriter->newItem() ) {

 		$request = new PxPayRequest();	
 		
 		$request->setAmountInput($txnData['AmountInput']);
  		$request->setTxnData1($txnData['TxnData1']);# whatever you want to appear
  		$request->setTxnData2($txnData['TxnData2']);# whatever you want to appear
  		$request->setTxnData3($txnData['TxnData3']);# whatever you want to appear
  		$request->setTxnType($TxnType);
  		$request->setInputCurrency("NZD");
  		$request->setMerchantReference($MerchantReference); # fill this with your order number
  		$request->setEmailAddress($txnData['EmailAddress']);
  		$request->setUrlFail($script_url);
  		$request->setUrlSuccess($script_url);   		
		  
  		$pxaccess = new PxAccess(PX_ACCESS_URL, PX_ACCESS_USERID, PX_ACCESS_KEY, PX_ACCESS_MAC_KEY);
  		
  		$request_string = $pxaccess->makeRequest($request);
  		redirectCleanly($request_string);
		
  		
	} else {
		return false;
	}
}

// function for updating the transaction in the database after it has completed
function finaliseTransaction($rsp, $db){
	
	$tableWriter = new CmsTableWriter(CMS_FORMAT,$db);
	$tableWriter->begin();
	
	// write the results to the database
	if ($rsp->getStatusRequired() == "1") {
		$result = "STATUS REQUIRED/ERROR";
	} else if ($rsp->getSuccess() == "1") {
		$result = "APPROVED";
	} else {
		$result = "DECLINED";
	}
	
	$MerchantReference = $rsp->getMerchantReference();
	
	$tableWriter->addField($tableWriter->primaryKey, $MerchantReference);
	$xml = $rsp->getMifXML();
	$tableWriter->addField("content",$xml);

	if (($responseText = $rsp->getResponseText())){
		$tableWriter->addField("shortdescription", $responseText);
	}
	if (($amountSettlement = $rsp->getAmountSettlement())){
		$tableWriter->addField("authordepartment", $amountSettlement);
	}
	if (($authCode = $rsp->getAuthCode())){
		$tableWriter->addField("authorbio", $authCode);
	}
	if (($cardName = $rsp->getCardName())){
		$tableWriter->addField("organisationalname", $cardName);
	}
	$tableWriter->saveItem();

	return array("status"=>$result, "MifMessage"=>$xml);
  
}


function checkProcess($MerchantReference, $db){
	
	$tableWriter = new CmsTableWriter(CMS_FORMAT,$db);
	$row = $tableWriter->readItem($MerchantReference);
	$status = $row->shortdescription;
	
	if($status != "PROCESSING")
		return false;
	else
		return true;
}


?>
